<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>图书列表管理</el-breadcrumb-item>
      <el-breadcrumb-item>图书信息列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 定义卡片 -->
    <el-card class="box-card">

      <!-- 定义第一行数据 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryBookInfo.query" clearable @clear="getBookList">
            <el-button slot="append" icon="el-icon-search" @click="getBookList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="toAddBook">添加图书</el-button>
        </el-col>
      </el-row>

      <!-- 获取图书列表数据 -->
      <el-table :data="bookList" style="width: 100%" stripe border>
        <el-table-column type="id" label="序号" width="50px">
        </el-table-column>
        <el-table-column type="name" label="书籍名称" width="50px">
        </el-table-column>
        <el-table-column type="author" label="书籍作者" width="50px">
        </el-table-column>
        <el-table-column type="press" label="书籍出版社" width="50px">
        </el-table-column>
        <el-table-column prop="title" label="图书标题" width="400px">
        </el-table-column>
        <el-table-column prop="sellPoint" label="卖点信息" width="300px">
        </el-table-column>
        <!-- 使用过滤器修改时间 在main.js中定义过滤器 -->
        <el-table-column prop="price" label="价格(元)" width="80px">
          <template slot-scope="scope">
            {{scope.row.price | priceFormat}}
          </template>
        </el-table-column>
        <el-table-column prop="num" label="数量" width="80px">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="80px">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"
              @change="updateStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
       <!-- <el-table-column prop="updated" label="更新时间" width="180px" :formatter="formatDate">
        </el-table-column> -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="updateBookBtn(scope.row)">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteBookBtn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <!-- 定义分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryBookInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryBookInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>

      <el-dialog title="修改图书信息" :visible.sync="updateDialogVisible" width="50%" @close="closeUpdateDialog">
        <el-form :model="updateBook" ref="updateBookRef" label-width="100px">
          <el-form-item label="序号" prop="id">
            <el-input v-model="updateBook.id"  disabled ></el-input>
          </el-form-item>
          <el-form-item label="图书标题" prop="title">
            <el-input v-model="updateBook.title"></el-input>
          </el-form-item>
          <el-form-item label="卖点信息" prop="sellPoint">
            <el-input v-model="updateBook.sellPoint"></el-input>
          </el-form-item>
          <el-form-item label="价格(元)" prop="price">
            <el-input v-model="updateBook.price"></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="num">
            <el-input v-model="updateBook.num"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="updateDialogVisible = false" >取 消</el-button>
          <el-button type="primary" @click="updateBooks">确 定</el-button>
        </span>
      </el-dialog>

    </el-card>



  </div>
</template>

<script>
  export default {
    data() {
      return {
        //1.定义图书列表信息
        bookList: [],
        //2.定义分页对象
        queryBookInfo: {
          query: '', //定义查询参数
          pageNum: 1,
          pageSize: 10
        },
        updateBook:{
        },
        total: 0,
        updateDialogVisible : false
      }
    },
    created() {
      //1.获取图书列表数据
      this.getBookList()
    },
    methods: {
      //实现图书信息分页查询
      async getBookList() {
        const {
          data: result
        } = await this.$http.get("/book/getBookList", {
          params: this.queryBookInfo
        })
        if (result.status !== 200) return this.$message.error("图书列表查询失败")
        this.bookList = result.data.rows
        this.total = result.data.total
      },
      //通过JS格式化时间
      formatDate(row, column, cellValue, index) {
        let date = new Date(cellValue)
        let year = date.getFullYear()
        let month = (date.getMonth() + 1 + '').padStart(2, '0')
        let day = (date.getDate() + '').padStart(2, '0')
        let HH = (date.getHours() + '').padStart(2, '0')
        let MM = (date.getMinutes() + '').padStart(2, '0')
        let SS = (date.getSeconds() + '').padStart(2, '0')
        return year + '-' + month + '-' + day + ' ' + HH + ":" + MM + ":" + SS
      },
      //条数变化时 调用
      handleSizeChange(size) {
        this.queryBookInfo.pageSize = size
        this.getBookList()
      },
      //页码值变化时 调用
      handleCurrentChange(current) {
        this.queryBookInfo.pageNum = current
        this.getBookList()
      },
      async updateStatus(book) {
        const {
          data: result
        } = await this.$http.put("/book/updateBookStatus", {
          id: book.id,
          status: book.status
        })
        if (result.status !== 200) return this.$message.error("更新状态失败")
        this.$message.success("更新状态成功")
      },
      async deleteBookBtn(book) {
        //消息确认框
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //根据id删除数据
          const {
            data: result
          } = await this.$http.delete("/book/deleteBookById", {
            params: {
              id: book.id
            }
          })
          if (result.status !== 200) return this.$message.error("图书删除失败")
          this.$message.success("图书删除成功")
          //重新获取图书列表信息
          this.getBookList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      //转向到图书新增页面
      toAddBook(){
        this.$router.push("/book/addBook")
      },
      async updateBookBtn(bookList){
        this.updateDialogVisible = true;
        let {data} = await this.$http.get(`/book/getById/${bookList.id}`)
        if(data.status !== 200) return this.$message.error("查询失败")
        this.updateBook = data.data
        console.log("扩展案例,自己实现 只需要修改 标题/卖点/价格/数量")
      },

      async updateBooks(){
        this.updateBook.price = this.updateBook.price * 100
        let {data} = await this.$http.put("/book/updateBooks",this.updateBook)
        if(data.status !== 200)return this.$message.error("更改失败")
        this.$message.success("修改成功")
        this.getBookList()
        this.updateDialogVisible = false;
      },
      closeUpdateDialog(){
        this.$refs.updateBookRef.resetFields()
      },
    }
  }
</script>
